package com.jcrj.action.mail;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.ODraftMail;
import com.jcrj.entity.OInMail;
import com.jcrj.entity.OMailLog;
import com.jcrj.entity.OMailReceiver;
import com.jcrj.entity.OUser;
import com.jcrj.service.MailService;
import com.jcrj.service.SysLogService;
import com.jcrj.util.Base64;
import com.jcrj.util.FileIo;

public class MailAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 729780409453667721L;
	private MailService mailService;
	private String title;
	private String name;
	private String content;
	private List<OMailReceiver> inmailList;
	private List<ODraftMail> draftList;
	private List<OInMail> mailList;
	private List<OMailLog> readList;
	private List notReadList;
	private long pageCount;
	private long totalCount;
	private String startDate;
	private String endDate;
	private String statu;
	private String[] ids;
	private OInMail inMail;
	private ODraftMail draftMail;
	private int mailId;
	private int userId;
	private String reply;
	private SysLogService sysLogService;

	public int getUserId() {
		return this.userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String mail() {
		OUser user = this.mailService.getUserById(this.userId);
		this.draftMail = new ODraftMail();
		this.draftMail.setReceiverNames(user.getName());
		this.draftMail.setReceiverIds(user.getId().toString());
		return "success";
	}

	public String mailWrite() {
		return "success";
	}

	public String mailSuccess() {
		return this.statu;
	}

	public String saveGraftMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.draftMail.setContent(this.content);
			this.draftMail.setCreateTime(new Date());
			this.draftMail.setSendId(user.getId());
			this.draftMail.setSendName(user.getName());
			this.mailService.saveDraftMail(this.draftMail);
			this.sysLogService.saveLog("邮件", user.getName(), "添加了" + this.draftMail.getTitle() + "邮件草稿");
		} catch (RuntimeException e) {
			e.printStackTrace();
		}

		return "success";
	}

	public String saveInMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.draftMail.setContent(this.content);
			if ((this.draftMail.getStatus() == null) || ("".equals(this.draftMail.getStatus()))) {
				this.draftMail.setStatus("普通");
			}
			this.mailService.saveInMail(this.draftMail, user, this.reply);
			this.sysLogService.saveLog("邮件", user.getName(), "发送了一封" + this.draftMail.getTitle() + "的邮件");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String getInMailList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			String agent = this.sysLogService.getAgentUser(user);
			this.totalCount = this.mailService.getInMailCount(this.title, this.name, this.startDate, this.endDate, agent).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			if (super.getCurrentPage() > this.pageCount) {
				super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
			}
			this.inmailList = this.mailService.getInMailList(this.title, this.name, this.startDate, this.endDate, agent, super.getCurrentPage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String getDraftMailList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			if (user != null) {
				this.totalCount = this.mailService.getDraftMailCount(this.title, this.startDate, this.endDate, user.getId().intValue()).longValue();
				this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
				if (super.getCurrentPage() > this.pageCount) {
					super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
				}
				this.draftList = this.mailService.getDraftMailList(this.title, this.startDate, this.endDate, user.getId().intValue(), super.getCurrentPage());
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String getSendMailListAll() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			if (user != null) {
				this.totalCount = this.mailService.getSendMailCount(this.title, this.name, this.startDate, this.endDate, user.getId().intValue()).longValue();
				this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
				if (super.getCurrentPage() > this.pageCount) {
					super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
				}
				this.mailList = this.mailService.getSendMailList(this.title, this.name, this.startDate, this.endDate, user.getId().intValue(),
						super.getCurrentPage());
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String getDeleteMailList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			if (user != null) {
				this.totalCount = this.mailService.getDeleteMailCount(this.title, this.startDate, this.endDate, user.getId().intValue()).longValue();
				this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
				if (super.getCurrentPage() > this.pageCount) {
					super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
				}
				this.inmailList = this.mailService.getDeleteMailList(this.title, this.startDate, this.endDate, user.getId().intValue(), super.getCurrentPage());
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String getDraftMailUpdate() {
		this.draftMail = this.mailService.getDraftMail(this.mailId);
		return "success";
	}

	public String getOInMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.inMail = this.mailService.updateGetInMail(this.mailId, user);
			if ((this.inMail.getContent() != null) && (!"".equals(this.inMail.getContent())))
				setContent(Base64.decode(this.inMail.getContent()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String mailUpOrDownLook() {
		OUser user = (OUser) super.getSession().get("userInfo");
		Integer mails = null;
		try {
			mails = this.mailService.getMailNextOrUpId(user.getId().intValue(), this.mailId, this.statu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if ((mails != null) && (!"".equals(mails))) {
			this.mailId = mails.intValue();
			return "success";
		}
		return "list";
	}

	public String updateInMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.mailService.updateInMail(this.ids, user);
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String completeDeleteMail() {
		OUser user = (OUser) super.getSession().get("userInfo");
		Integer mails = null;
		try {
			mails = this.mailService.getMailNextOrUpId(user.getId().intValue(), this.mailId, this.statu);
			this.ids = new String[] { String.valueOf(this.mailId) };
			this.mailService.deleteInMail(this.ids, user);
		} catch (Exception localException) {
		}
		if ((mails != null) && (!"".equals(mails))) {
			this.mailId = mails.intValue();
			return "success";
		}
		return "list";
	}

	public String deleteInMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.mailService.deleteInMail(this.ids, user);
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteDraftMail() {
		try {
			this.mailService.deleteDraftMail(this.ids);
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteSendMail() {
		try {
			this.mailService.deleteOuttMail(this.ids);
			super.getResponse().getWriter().write("success");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getSendMailInfo() {
		this.inMail = this.mailService.getInMailInfo(this.mailId);
		if ((this.inMail.getContent() != null) && (!"".equals(this.inMail.getContent()))) {
			setContent(Base64.decode(this.inMail.getContent()));
		}
		return "success";
	}

	public String mailBack() {
		try {
			OInMail mail = this.mailService.getInMailInfo(this.mailId);
			this.draftMail = new ODraftMail();
			if ("back".equals(this.statu)) {
				this.draftMail.setTitle("Re:" + mail.getTitle());
				this.draftMail.setReceiverIds(mail.getSendId().toString());
				this.draftMail.setReceiverNames(mail.getSendName());
				setReply(mail.getId().toString());
			} else {
				this.draftMail.setTitle("Fw:" + mail.getTitle());
				this.draftMail.setFileId(mail.getFileId());
			}
			this.content = ("-------邮件信息-------<br/>发件人：" + mail.getSendName() + "<br/>发送日期：" + mail.getCreateTime().toLocaleString() + "<br/>收件人："
					+ mail.getReceiverName() + "<br/>标题：" + mail.getTitle());
			if ((mail.getContent() != null) && (!"".equals(mail.getContent())))
				this.content = (this.content + "<br/>" + new String(Base64.decode(mail.getContent())));
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String mailReadInfo() {
		this.readList = this.mailService.readUserList(this.mailId);
		this.notReadList = this.mailService.notReadUserList(this.mailService.getUserList(this.readList), this.mailId);
		return "success";
	}

	public String downLoadMail() {
		File file = this.mailService.getMailAllList(this.statu);
		FileIo.downloadLocal(super.getResponse(), file, "邮件.rar");
		return null;
	}

	public String getMailVolume() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			String volume = this.mailService.countMailVolume(user.getId().intValue());
			super.getResponse().getWriter().write(volume);
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String selectUser() {
		return "success";
	}

	public String mailRollBack() {
		try {
			boolean flag = this.mailService.addMailRollBck(this.mailId);
			super.getResponse().getWriter().write(String.valueOf(flag));
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public MailService getMailService() {
		return this.mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	public List<OInMail> getMailList() {
		return this.mailList;
	}

	public void setMailList(List<OInMail> mailList) {
		this.mailList = mailList;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<OMailReceiver> getInmailList() {
		return this.inmailList;
	}

	public void setInmailList(List<OMailReceiver> inmailList) {
		this.inmailList = inmailList;
	}

	public List<ODraftMail> getDraftList() {
		return this.draftList;
	}

	public void setDraftList(List<ODraftMail> draftList) {
		this.draftList = draftList;
	}

	public long getPageCount() {
		return this.pageCount;
	}

	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}

	public long getTotalCount() {
		return this.totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	public String getStartDate() {
		return this.startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return this.endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getStatu() {
		return this.statu;
	}

	public void setStatu(String statu) {
		this.statu = statu;
	}

	public String[] getIds() {
		return this.ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public OInMail getInMail() {
		return this.inMail;
	}

	public void setInMail(OInMail inMail) {
		this.inMail = inMail;
	}

	public ODraftMail getDraftMail() {
		return this.draftMail;
	}

	public void setDraftMail(ODraftMail draftMail) {
		this.draftMail = draftMail;
	}

	public int getMailId() {
		return this.mailId;
	}

	public void setMailId(int mailId) {
		this.mailId = mailId;
	}

	public SysLogService getSysLogService() {
		return this.sysLogService;
	}

	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}

	public List<OMailLog> getReadList() {
		return this.readList;
	}

	public void setReadList(List<OMailLog> readList) {
		this.readList = readList;
	}

	public List getNotReadList() {
		return this.notReadList;
	}

	public void setNotReadList(List notReadList) {
		this.notReadList = notReadList;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getReply() {
		return this.reply;
	}

	public void setReply(String reply) {
		this.reply = reply;
	}
}